package com.education.student.controller;

import com.education.model.*;
import io.swagger.models.auth.In;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.education.service.ICourseInfoService;
import com.education.service.IEvaluateService;
import com.education.service.IPartSectionService;
import com.education.service.IPlayVideoService;
import com.education.service.IStuCourseService;
import com.education.service.IStuIndexService;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 学生主页控制层
 *
 * @author 刘帅
 */
@Api(value = "/api/student", description = "学生端的相关操作")
@RequestMapping("/api/student")
@Controller
public class StuIndexController {

    /**
     * 日志记录类
     */
    private static Logger MYLOGGER = LogManager.getLogger(StuIndexController.class.getName());

    /**
     * 学生主页实现
     */
    @Reference
    private IStuIndexService stuIndexService;

    /**
     * 课程评价服务层
     */
    @Reference
    private IEvaluateService evaluateService;

    /**
     * 学习课程服务层
     */
    @Reference
    private IStuCourseService stuCourseService;

    /**
     * 小节服务层
     */
    @Reference
    private IPartSectionService partSectionService;

    /**
     * 课程详细信息
     */
    @Reference
    private ICourseInfoService courseInfo;

    /**
     * 播放视频
     */
    @Reference
    private IPlayVideoService playVideo;

    /**
     * 学生主页
     *
     * @param pageSize    每页显示条数
     * @param currentPage 当前页
     * @return 返回学生所有课程
     */
    @ApiOperation(notes = "query", httpMethod = "GET", value = "返回学生所有课程")
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    @ResponseBody
    public ResultDo<PageInfo<StuIndexDo>> queryCourse(int pageSize, int currentPage) {

        MYLOGGER.info("每页条数" + pageSize + currentPage);
        ResultDo<PageInfo<StuIndexDo>> resultDo = new ResultDo<PageInfo<StuIndexDo>>();
        resultDo = stuIndexService.queryCourse(1, currentPage, pageSize);

        return resultDo;
    }

    /**
     * 学生主页
     * 学生学籍表信息查询   马松
     *
     * @param stuId 当前学生登录ID
     * @return
     * @throws Exception
     */
    @RequestMapping("/queryMessage")
    @ResponseBody
    public ResultDo<List<StuMessageDo>> getStuMsg(Integer stuId,HttpSession session) throws Exception {

        MYLOGGER.info("----查询学生学籍信息ID：----4");
        //从登录学生session中取出登录学生ID
        Integer stuIds = (Integer) session.getAttribute("stuId");

        List<StuMessageDo> stuMsgDo = stuIndexService.queryStuById(stuIds);

        ResultDo<List<StuMessageDo>> res = new ResultDo<List<StuMessageDo>>();
        res.setResData(stuMsgDo);
        MYLOGGER.info("----学生学籍信息：" + res);
        return res;
    }


    /**
     * 学生主页
     * 学生考试成绩   马松
     *
     * @param studentId 当前学生登录的ID
     * @return
     * @throws Exception
     */

    @RequestMapping("/queryStuScore")
    @ResponseBody
    public ResultDo<List<StudentScoreModel>> getStudentScore(Integer studentId,HttpSession session) throws Exception {
        MYLOGGER.info("----查看当前登录学生考试成绩,当前ID：" + 4);
        //获取session。得到登录ID
        Integer stuIds = (Integer) session.getAttribute("stuId");
        List<StudentScoreModel> stuScoreModel = stuIndexService.queryStuScoreById(stuIds);
        ResultDo<List<StudentScoreModel>> res = new ResultDo<List<StudentScoreModel>>();
        res.setResData(stuScoreModel);
        MYLOGGER.info("----学生考试成绩：" + res);
        return res;
    }

    /**
     * 学生端学生考试详情信息
     *  -----  马松
     * @param studentId
     * @return
     * @throws Exception
     */
    @RequestMapping("/queryStuDetail")
    @ResponseBody
    public ResultDo<StudentScoreModel> getStuDetail(Integer studentId,HttpSession session) throws Exception{

        MYLOGGER.info("学生考试详情页面，学生ID:"+4);
        Integer stuIds = (Integer) session.getAttribute("stuId");
        ResultDo<StudentScoreModel> res = stuIndexService.qrerystuIdDetail(stuIds);
        return res;
    }


    /**
     * 课程评价
     *
     * @param evaluate 评价实体
     * @param session
     * @return 添加结果
     */
    @ApiOperation(notes = "addEvaluate", httpMethod = "POST", value = "添加课程评价")
    @RequestMapping(path ="/addEvaluate",method = RequestMethod.POST)
    @ResponseBody
    public ResultDo<String> addEvaluate(@RequestBody ClassEvaluate evaluate,HttpSession session) {
        ResultDo<String> rs = new ResultDo<>();
        int  stuId=(int)session.getAttribute("stuId");
        evaluate.setStudentId(stuId);
        int flag = evaluateService.addEvaluate(evaluate);
        if(flag>0){
            rs.setResCode(0);
            rs.setResMsg("添加成功");
        }else {
            rs.setResCode(101);
            rs.setResMsg("添加失败");
        }
        return rs;
    }

    /**
     * 学习课程
     *
     * @param courseId    课程编号
     * @param pageSize    每页显示条数
     * @param currentPage 当前页
     * @return 返回该课程所有章节
     */
    @ApiOperation(notes = "sectionPage", httpMethod = "GET", value = "返回该课程所有章节")
    @RequestMapping("/sectionPage")
    @ResponseBody
    public ResultDo<PageInfo<SectionDo>> querySection(int courseId, int pageSize, int currentPage) {

        MYLOGGER.info("每页条数" + pageSize + currentPage);
        ResultDo<PageInfo<SectionDo>> resultDo = new ResultDo<PageInfo<SectionDo>>();

        resultDo = stuCourseService.querySection(courseId, currentPage, pageSize);

        return resultDo;
    }

    /**
     * 所有小节
     *
     * @param courseId   章节编号
     * @param pageSize    每页显示条数
     * @param currentPage 当前页
     * @return 返回该章节所有小节
     */
    @ApiOperation(notes = "stuCourse/detail", httpMethod = "GET", value = "返回该章节所有小节")
    @RequestMapping("/stuCourse/detail")
    @ResponseBody
    public ResultDo<PageInfo<PartSectionModel>> queryPartSection(Integer courseId, Integer pageSize, Integer currentPage) {

        //MYLOGGER.info("每页条数"+pageSize+currentPage);
        ResultDo<PageInfo<PartSectionModel>> resultDo = new ResultDo<PageInfo<PartSectionModel>>();
        //MYLOGGER.info("章节编号"+courseId);
        resultDo = partSectionService.queryCourse(courseId, currentPage, pageSize);
        return resultDo;
    }

    /**
     * 课程详细信息
     *
     * @param partId 小节编号
     * @return 返回该小节对应的课程详细信息
     */
    @ApiOperation(notes = "getCourseInfo", httpMethod = "GET", value = "返回该小节对应的课程详细信息")
    @RequestMapping("/getCourseInfo")
    @ResponseBody
    public ResultDo<CourseDo> findCourseInfo(int partId) {

        MYLOGGER.info("章节编号" + partId);
        ResultDo<CourseDo> resultDo = new ResultDo<CourseDo>();

        resultDo = courseInfo.getCourseInfo(partId);

        return resultDo;
    }

    /**
     * 播放视频
     *
     * @param partId 小节编号
     * @return 返回该小节对应的视频
     */
    @ApiOperation(notes = "getVideo", httpMethod = "GET", value = "返回该小节对应的视频")
    @RequestMapping("/getVideo")
    @ResponseBody
    public ResultDo<VideoModel> getVideo(int partId) {

        MYLOGGER.info("章节编号" + partId);
        ResultDo<VideoModel> resultDo = new ResultDo<VideoModel>();

        resultDo = playVideo.getVideo(partId);

        return resultDo;
    }

    /**
     * by 曹朋 2017.8.16
     * 查询登录学生学习的课程
     * @param session
     * @return
     * @throws Exception
     */
    @ApiOperation(notes = "selectCourse", httpMethod = "GET", value = "返回该学生对应的课程")
    @RequestMapping("/selectCourse")
    @ResponseBody
    public ResultDo<List<StuIndexDo>> selectCourse(HttpSession session) throws Exception {
        ResultDo< List<StuIndexDo>> rs = new ResultDo<>();
        int stuid = (int) session.getAttribute("stuId");
        List<StuIndexDo> list = stuIndexService.selectCurrCourse(stuid);
        if(list.size()<=0){
            rs.setResCode(123);
            rs.setResMsg("没有数据");
        }
        rs.setResData(list);
        return rs;
    }

    /**
     * by 曹朋 2017.8.17
     * 视频观看一次，访问次数加1
     * @return
     * @throws Exception
     */
    @ApiOperation(notes = "partFrequenc", httpMethod = "GET", value = "返回更新条数")
    @RequestMapping("/partFrequence")
    @ResponseBody
    public ResultDo<Integer> partFrequenc(Integer partId) throws Exception {
        ResultDo<Integer> rs = new ResultDo<>();
        rs.setResData(stuIndexService.partFrequence(partId));
        return rs;
    }
}
